;
(function() {
  new Vue({
    el: '#report_bi',
    data: {
      quanxian: JSON.parse(localStorage.getItem('permissions')),
      arr: [],
      data: [],
      pagecount: ['10'],
      num: 0,
      pageTcount: [{name: '10', code: '10'}, {name: '15', code: '15'}, {name: '20', code: '20'}],
      month: 12,
      ymflag: false,
      getyear: null,
      getmonth: null,
      timebill: new Date().getFullYear() + '-' + toZero(new Date().getMonth()+1),
      keyword: '',
      setyear: null,
      setmonth: null,
      tHeadTitle: null,
    },
    filters: {
      toZero: function (n) {
        return n < 10 ? '0' + n : '' + n
      },
      UTCdate: function (time) {
        return hlDateUtil.formatUTCDate(time, 'yyyy-MM-dd')
      },
      formatNum:function (num) {
        return hlNumberUtil.formatNum(num)
      },
      nullundefinded (item) {
        if (!item) return '--';
        return item.join('、')
      }
    },
    computed: {
      yearli () {
        var result = [];
        var nowYear = new Date().getFullYear();
        var nowYear = nowYear - 15;
        for (var i = 0; i < 100; i++) {
          nowYear++;
          result.push(nowYear)
        }
        return result;
      },
    },
    methods: {
      resetData () {
        this.timebill = new Date().getFullYear() + '-' + toZero(new Date().getMonth()+1);
        this.keyword = '';
        this.pageIn(1, this.arr)
      },
      cancelBubbleTrue () {
        event.cancelBubble = true
        event.stopPropagation()
      },
      ymshow () {
        this.ymflag = !this.ymflag;
        if (this.ymflag) {
          if (this.timebill) {
            this.getyear = this.timebill.split('-')[0]
            this.getmonth = this.timebill.split('-')[1]
            setTimeout(() => {
              this.$refs.yearhtml.innerHTML = this.getyear
              this.$refs.monthhtml.innerHTML = this.getmonth
            }, 200)
          }
        }
      },
      getyearfn (item) {
        this.getyear = item
        this.timebill = this.getyear + '-' + toZero(this.getmonth)
      },
      getmonthfn (item) {
        this.getmonth = toZero(item)
        this.timebill = this.getyear + '-' + toZero(item)
      },
      searchget () {
        if (!this.timebill) {
          hl.toast('请选择时间')
          return
        }
        this.pageIn(1, this.arr)
      },
      pageFY: function (arr) {
        this.pagecount = [arr[0].code];
        var _this = this;
        setTimeout(function() {
          _this.pageIn()
        }, 200)
      },
      pageIn: function() {
        var _this = this;
        var pages = {
          "turnPage": function(n) {
            _this.getData(n, _this.arr);
          }
        }
        $("#page").initPage(1, 1, pages.turnPage);
      },
      getData: function(page, arr) {
        var _this = this;
        hlhttp.get({
          url: 'report/month_bill',
          data: {
            month: this.timebill,
            keyword: this.keyword,
            project_ids: _this.arr[0].toString() || _this.arr[1].toString(),
            page: page,
            page_count: _this.pagecount[0]
          },
          success: function(res) {
            _this.num = res.totalCount;
            _this.data = res.monthBill;
            _this.setyear = _this.getyear || new Date().getFullYear();
            _this.setmonth = _this.getmonth || toZero(new Date().getMonth()+1);
            _this.tHeadTitle = _this.setyear + '年' + _this.setmonth + '月应收账单'

            _this.data.forEach(el => {
              let projectName = [], buildingName = [] , floorName = [] , unitName = [] , unitAcreage = []
              el.unitInfos.forEach(el1 => {
                projectName.push(el1.projectName)
                buildingName.push(el1.buildingName)
                floorName.push(el1.floorName)
                unitName.push(el1.unitName)
                unitAcreage.push(el1.unitAcreage)
              })

              el['projectName'] = projectName.join('$nbsp')
              el['buildingName'] = buildingName.join(' ')
              el['floorName'] = floorName.join(' ')
              el['unitName'] = unitName.join(' ')
              el['unitAcreage'] = unitAcreage.join(' ')

              el['depositPeriod'] = '押' + el['depositPeriod'] + ' 付' + el['paymentPeriod']
              el['billBeginDate'] = hlDateUtil.formatUTCDate(el['billBeginDate'], 'yyyy-MM-dd')
              el['billEndDate'] = hlDateUtil.formatUTCDate(el['billEndDate'], 'yyyy-MM-dd')
              el['billBalanceTime'] = hlDateUtil.formatUTCDate(el['billBalanceTime'], 'yyyy-MM-dd')

              el['billAmountReceive '] = hlNumberUtil.formatNum(el['billAmountReceive'])
              el['billAmountPayed '] = hlNumberUtil.formatNum(el['billAmountPayed'])
              el['billAmountTpay '] = hlNumberUtil.formatNum(el['billAmountTpay'])
              el['invoiceAmount '] = hlNumberUtil.formatNum(el['invoiceAmount'])
              el['taxExcludedAmount '] = hlNumberUtil.formatNum(el['taxExcludedAmount'])
              el['taxAmount'] = hlNumberUtil.formatNum(el['taxAmount'])

              el['contractInvoiceAmount'] = hlNumberUtil.formatNum(el['contractInvoiceAmount'])
              el['contractTaxExcludedAmount'] = hlNumberUtil.formatNum(el['contractTaxExcludedAmount'])
              el['contractTaxAmount'] = hlNumberUtil.formatNum(el['contractTaxAmount'])
              el['contractAmountReceive'] = hlNumberUtil.formatNum(el['contractAmountReceive'])
              el['contractAmountPayed'] = hlNumberUtil.formatNum(el['contractAmountPayed'])
              el['contractAmountTpay'] = hlNumberUtil.formatNum(el['contractAmountTpay'])



            })

            $("#page").initPage(res.totalPage, page, function(n) {
              if (page == n) {
                return
              }
              _this.getData(n, _this.arr);
            });
          }
        })
      },
      excl: function () {
        if (!this.arr[0].length) {
          hl.toast('请选择项目')
          return
        }
        var _this = this;
        hlhttp.get({
          baseUrl: exclUrl,
          url: 'resource/month_bill_export',
          data: {
            project_ids: _this.arr[0].toString() || _this.arr[1].toString(),
            month: this.timebill,
            keyword: this.keyword
          },
          success: (res) => {
            var a = document.createElement('a');
            a.href = res.excel_url;
            $('body').append($(a))
            a.click();
            setTimeout(function() {
              $(a).remove()
            }, 100);
          }
        })

      }
    },
    mounted: function() {
      var _this = this;
      hlSelectUi(true)
      loginPrem.loginList()
      getProduct(function (data) {
        _this.arr = data;
        _this.pageIn(1, data)
      },function (data) {
        _this.arr = data;
        _this.pageIn(1, data)
      },function (data) {
        _this.arr = data;
        _this.pageIn(1, data)
      })

      document.addEventListener('click', (e) => {
        if (e.srcElement.className.indexOf('timebill') == -1) {
          this.ymflag = false
        }
      }, true)
    }
  })

  function toZero (n) {
    return Number(n) < 10 ? '0' + Number(n) : '' + Number(n)
  }
}())
